// 云函数入口文件
const cloud = require('wx-server-sdk')
const mysql = require('mysql2/promise')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  var teacherID = event.teacherID;
  var name = event.name;
  var phone = event.phone;
  var address = event.address;
  var password = event.password;
  var sex = event.sex;
  try {
    const connection = await mysql.createConnection({
      host: "47.115.22.254",
      port: 3306,
      database: "EPMS",
      user: "root",
      password: "1234"
    })
    var selectsql = 'SELECT * FROM TUSER WHERE teacherID="'+teacherID+'";'
    var [rows, mat] = await connection.execute(selectsql);
    if(rows.length){
      return '该用户已经注册过了'
    }else{
      var insertsql = 'INSERT INTO TUSER(teacherID,name,password,address,phone,sex) VALUES("' + teacherID + '","' + name + '","' + password + '","' + address + '","' + phone + '","' + sex + '");';
      await connection.execute(insertsql);
      return '注册成功'
      conection.end()
    }
    


  } catch (err) {
    return err
  }
}